function loadEntity(){
	var info = window.info;
	
	$('name-field').value = info['name'];
	$('parent-field').value = info['parent'];
	$('location-field').value = info['location'];
	$('owner-field').value = info['owner'];
	
	$('id-field').value = info['id'];
	$('type-select').selectedIndex = info['entity_type'];
	
	var verbsSelect = $('verbs-select');
	verbsSelect.options.length = 0;
	for(index in info['verbs']){
		var verb = info['verbs'][index];
		verbsSelect.options[index] = new Option(verb, verb);
	}
	
	var propsSelect = $('props-select');
	propsSelect.options.length = 0;
	for(index in info['properties']){
		var prop = info['properties'][index];
		propsSelect.options[index] = new Option(prop, prop);
	}
}

function saveEntity(){
	var info = {};
	info['name'] = $('name-field').value;
	info['parent'] = $('parent-field').value;
	info['location'] = $('location-field').value;
	info['owner'] = $('owner-field').value;

	info['entity_type'] = $('type-select').selectedIndex;
	
	window.resultDeferred.callback(info);
	window.close();
}

function cancelEntity(){
	window.resultDeferred.callback(null);
	window.close();
}

function reloadEntity(){
	var connector = window.clientWindow.getConnector();
	var deferred = connector.callRemote('get_entity_attributes', window.info['id']);
	deferred.addCallback(function(response){
		window.info = response;
		loadEntity();
	});
	return deferred;
}

function requestEntityEditor(id){
	var connector = window.clientWindow.getConnector();
	var deferred = connector.callRemote('req_entity_editor', id);
	return deferred;
}

function addVerb(){
	var connector = window.clientWindow.getConnector();
	var verb_name = prompt("What do you want to call this verb?");
	if(! verb_name.match(/^\S+$/)){
		alert("Sorry, verb names cannot have spaces.");
	}
	else{
		var deferred = connector.callRemote('req_code_editor', window.info['id'], verb_name);
		deferred.addCallback(function(response){
			reloadEntity();
		});
		return deferred;
	}
}

function removeVerb(){
	if($('verbs-select').selectedIndex != -1){
		var verb_name = $('verbs-select').value;
		if(confirm("Are you sure you want to delete the verb '" + verb_name + "'?")){
			var connector = window.clientWindow.getConnector();
			var deferred = connector.callRemote('remove_verb', window.info['id'], verb_name);
			deferred.addCallback(function(response){
				reloadEntity();
			});
			return deferred;
		}
	}
}

function requestVerbEditor(item){
	var connector = window.clientWindow.getConnector();
	var deferred = connector.callRemote('req_code_editor', window.info['id'], item);
	deferred.addCallback(function(response){
		reloadEntity();
	});
	return deferred;
}

function addProperty(){
	var connector = window.clientWindow.getConnector();
	var prop_name = prompt("What do you want to call this property?");
	var deferred = connector.callRemote('req_property_editor', window.info['id'], prop_name);
	deferred.addCallback(function(response){
		reloadEntity();
	});
	return deferred;
}

function removeProperty(){
	if($('props-select').selectedIndex != -1){
		var prop_name = $('props-select').value;
		if(confirm("Are you sure you want to delete the property '" + prop_name + "'?")){
			var connector = window.clientWindow.getConnector();
			var deferred = connector.callRemote('remove_property', window.info['id'], prop_name);
			deferred.addCallback(function(response){
				reloadEntity();
			});
			return deferred;
		}
	}
}
function requestPropertyEditor(item){
	var connector = window.clientWindow.getConnector();
	var deferred = connector.callRemote('req_property_editor', window.info['id'], item);
	deferred.addCallback(function(response){
		reloadEntity();
	});
	return deferred;
}

function requestACLEditor(){
	var connector = window.clientWindow.getConnector();
	var deferred = connector.callRemote('req_acl_editor', window.info['id'], null);
	return deferred;
}